<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: easyui-header('代码生成列表')" />
</head>
<body class="white-bg">
    <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-head">
				<div class="easyui-panel">
					<form id="gen-form">
						<div class="select-list">
							<ul>
								<li>
									<label>表名称：</label>
									<input type="text" name="tableName" class="easyui-textbox" data-options="prompt:'请输入表名称'"/>
								</li>
								<li>
									<label>表描述：</label>
									<input type="text" name="dictType" class="easyui-textbox" data-options="prompt:'请输入表描述'"/>
								</li>
								<li>
									<label>创建时间： </label>
									<input type="text" id="startTime" class="easyui-datebox" style="width: 110px;" data-options="prompt:'开始时间'" name="params[beginTime]"/>
									<span>-</span>
									<input type="text" id="endTime" class="easyui-datebox" style="width: 110px;" data-options="prompt:'结束时间'" name="params[endTime]"/>
								</li>
								<li>
									<a class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="$.easyUIDataGrid.search()" >查询</a>
									<a class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="$.easyUIDataGrid.reset()" >重置</a>
								</li>
							</ul>
						</div>
					</form>
				</div>
			</div>
			<div class="col-sm-12 search-body">
			     <div id="toolbar" role="group">
		            <a class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="javascript:batchGenCode()" shiro:hasPermission="tool:gen:code">生成</a>
		            <a class="easyui-linkbutton" data-options="iconCls:'icon-import'" onclick="importTable()">导入</a>
		            <a class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="$.easyUIOperate.editTab()" shiro:hasPermission="tool:gen:edit">修改</a>
		            <a class="easyui-linkbutton" data-options="iconCls:'icon-delete'" onclick="$.easyUIOperate.removeAll()" shiro:hasPermission="tool:gen:remove">删除</a>
		        </div>
			    <table id="dg" class="easyui-datagrid"></table>
			</div>
		</div>
	</div>
	<th:block th:include="include :: easyui-footer" />
<!-- 	<th:block th:include="include :: bootstrap-table-export-js" /> -->
	<script th:src="@{/ajax/libs/highlight/highlight.min.js}"></script>
	<script th:inline="javascript">
		var editFlag = [[${@permission.hasPermi('tool:gen:edit')}]];
		var removeFlag = [[${@permission.hasPermi('tool:gen:remove')}]];
		var previewFlag = [[${@permission.hasPermi('tool:gen:preview')}]];
		var codeFlag = [[${@permission.hasPermi('tool:gen:code')}]];
    </script>
	<script th:inline="none">
		var prefix = ctx + "tool/gen";
		$(function() {
		    var options = {
		        url: prefix + "/list",
		        updateUrl: prefix + "/edit/{id}",
		        removeUrl: prefix + "/remove",
		        modalName: "生成配置",
		        idField: 'tableId',
		        sortName: "createTime",
		        sortOrder: "desc",
		        columns:[[
					{
						field:'ck',
						checkbox:true,
						width:30
					},
					{
			            field: 'tableId',
			            title: '编号',
		            	align: 'center',
		            	hidden:true,
						width: 20
			        },
			        {
			        	field : 'tableName', 
		        	  	title: '表名称',
			            align: 'center',
		          		width: 80
			        },
			        {
			        	field : 'tableComment', 
			        	title: '表描述',
			            align: 'center',
		          		width: 110
			        },
			        {
			        	field : 'className', 
			        	title: '实体类名称',
			            align: 'center',
		          		width: 80
			        },
			        {
			        	field : 'createTime', 
		        	 	title: '创建时间',
			            align: 'center',
			            sortable: true,
		          		width: 110
			        },
			        {
			        	field : 'updateTime', 
		        	 	title: '更新时间',
			            align: 'center',
			            sortable: true,
		          		width: 110
			        },
			        {
			        	field:'action',
			            title: '操作',
			            align: 'center',
		          		width: 180,
			            formatter: function(value, row, index) {
		            	 	var actions = [];
			                actions.push('<a class="btn btn-info btn-xs ' + previewFlag + '" href="javascript:void(0)" onclick="preview(\'' + row.tableId + '\')"><i class="fa fa-search"></i>预览</a> ');
			                actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.easyUIOperate.editTab(\'' + row.tableId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
			                actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.easyUIOperate.remove(\'' + row.tableId + '\')"><i class="fa fa-remove"></i>删除</a> ');
			                actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="synchDb(\'' + row.tableName + '\')"><i class="fa fa-refresh"></i>同步</a> ');
			                actions.push('<a class="btn btn-primary btn-xs ' + codeFlag + '" href="javascript:void(0)" onclick="genCode(\'' + row.tableName + '\',\'' + row.genType + '\')"><i class="fa fa-bug"></i>生成代码</a> ');
			                return actions.join('');
			            }
			        }
				]]
		        
		    };
		    $.easyUIDataGrid.init(options);
		});
		// 预览代码
		function preview(tableId) {
			var preViewUrl = prefix + "/preview/" + tableId;
			$.modal.loading("正在加载数据，请稍后...");
			$.get(preViewUrl, function(result) {
				if (result.code == web_status.SUCCESS) {
					 var items = [];
		                $.each(result.data, function(index, value) {
		                	value = value.replace(/</g, "&lt;");
		                	value = value.replace(/>/g, "&gt;");
		                    var templateName = index.substring(index.lastIndexOf("/") + 1, index.length).replace(/\.vm/g, "");
		                    if(!$.common.equals("sql", templateName) && !$.common.equals("tree.html", templateName) && !$.common.equals("sub-domain.java", templateName)){
			                    items.push({
			                        title: templateName , content: "<pre class=\"layui-code\">" + value + "</pre>"
			                    })
		                    }
		                });
		                top.layer.tab({
		                    area: ['90%', '90%'],
		                    shadeClose: true,
		                    tab: items
		                });
				} else {
					$.modal.alertError(result.msg);
				}
				$.modal.closeLoading();
			});
		}
		
		// 生成代码
		function genCode(tableName, genType) {
			$.messager.confirm("确认", "确定要生成【" + tableName + "】表代码吗？", function () {
				if(genType === "0") {
			    	location.href = prefix + "/download/" + tableName;
			        layer.msg('执行成功,正在生成代码请稍后…', { icon: 1 });
				} else if(genType === "1") {
					$.easyUIOperate.get(prefix + "/genCode/" + tableName);
				}
 	        });
		}
		
		// 同步数据库
		function synchDb(tableName){
			$.messager.confirm("确认", "确认要强制同步【" + tableName + "】表结构吗？", function (isTrue) {
 	        	if (isTrue) {
 	        		$.easyUIOperate.get(prefix + "/synchDb/" + tableName);
 	        	}
 	        });
		}
	
		// 批量生成代码
		function batchGenCode() {
			var selectRows = $("#dg").datagrid('getSelections');
    		if(selectRows.length == 0){
    			$.messager.alert("提示", "请至少选择一条要生成的数据！", 'warning');
    			return false;
    		}
    		var tableNamesArr = new Array();
 	        for(var i=0; i<selectRows.length; i++){
 	        	tableNamesArr.push(selectRows[i]["tableName"]);
 	        }
    		$.messager.confirm("确认", "确认要生成选中的【" + selectRows.length + "】条数据吗？", function (isTrue) {
 	        	if (isTrue) {
 	        		location.href = prefix + "/batchGenCode?tables=" + tableNamesArr.join();
 	        		layer.msg('执行成功,正在生成代码请稍后…', { icon: 1 });
 	        	}
 	        });
		}
		
		/* 导入表结构 */
		function importTable(){
			var url = prefix + "/importTable";
			// 皮肤缓存
        	var skin = storage.get("skin");
        	var panelWindowTheme = "";
        	var panelWindowHeaderTheme = "";
        	var panelWindowBodyTheme = "";
        	// 本地主题优先，未设置取系统配置
        	if($.common.isNotEmpty(skin)){
        		panelWindowTheme = "easyui-window-" + skin.split('|')[0];
        		panelWindowHeaderTheme = "easyui-window-header-" + skin.split('|')[0];
        		panelWindowBodyTheme = "easyui-window-body-" + skin.split('|')[0];
        	}
        	var dialogObj = $('<div id="selectTableDialog" style="overflow-x:hidden;"/>');
        	dialogObj.dialog({
        		href: url,
				iconCls: "icon-import",
				width : 800,
				height : ($(window).height() - 50),
				modal : true,
				maximizable: true,
				title : "选择用户",
				buttons : [ {
					text : '提交',
					iconCls : 'icon-save',
					handler : function() {
						var selectRows = $("#table_dg").datagrid('getSelections');
			    		if(selectRows.length == 0){
			    			$.messager.alert("提示", "请至少选择一条记录！", 'warning');
			    			return false;
			    		}
			    		var tableNamesArr = new Array();
			 	        for(var i=0; i<selectRows.length; i++){
			 	        	tableNamesArr.push(selectRows[i]["tableName"]);
			 	        }
						var data = {"tables": tableNamesArr.join()};
						var config = {
		        	        url: prefix + "/importTable",
		        	        type: "post",
		        	        dataType: "json",
		        	        data: data,
		        	        beforeSend: function () {
		        	        	$.modal.loading("正在处理中，请稍后...");
		        	        },
		        	        success: function(result) {
		        	        	if (result.code == web_status.SUCCESS) {
		                           	dialogObj.dialog('destroy');
		                        	$.messager.show({
		        						title : '提示',
		        						msg : result.msg,
		        						timeout: 1000
		        					});
		                        	var params = $.common.formToJSON("gen-form");
		                           	$('#dg').datagrid('load', params).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		                        } else if (result.code == web_status.WARNING) {
		                            $.messager.alert("提示", result.msg, 'warning');
		                        }  else {
		                        	$.messager.alert("提示", result.msg, 'error');
		                        }
		                    	$.modal.closeLoading();
		        	        }
		        	    };
		        	    $.ajax(config);
					}
				},{
					text : '关闭',
					iconCls : 'icon-cancel',
					handler : function() {
						dialogObj.dialog('destroy');
					}
				} ],
				onOpen : function(){
					$("#selectTableDialog").parents(".window").addClass(panelWindowTheme);
					$("#selectTableDialog").addClass(panelWindowBodyTheme);
					$("#selectTableDialog").siblings(".window-header").find(".panel-title").addClass(panelWindowHeaderTheme);
				},
				onClose : function() {
					dialogObj.dialog('destroy');
				}
			});
		}
	</script>
</body>
</html>